//
//  SearchResultViewController.m
//  DuBai
//
//  Created by darktm on 14-2-26.
//  Copyright (c) 2014年 darktm. All rights reserved.
//

#import "SearchResultViewController.h"
#import "HomeSearchResultCell.h"

#import "FiltrateViewController.h"

#import "SVPullToRefresh.h"

#import "TaskWithHomeSearch.h"
#import "SearchRsult.h"
#import "Search.h"

@interface SearchResultViewController ()<UITableViewDataSource,UITableViewDelegate,WYHttpTaskDelegate>
{
    SearchRsult *searchRsult;
    MBProgressHUD *mb;
    BOOL bRefresh;
}

@end

@implementation SearchResultViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.title = self.key;
    bRefresh = YES;
    __weak SearchResultViewController *weakSelf = self;
    [self.tableView addPullToRefreshWithActionHandler:^{
        [weakSelf refreshNew];
    } position:SVPullToRefreshPositionTop];
    
    [self.tableView addInfiniteScrollingWithActionHandler:^{
        [weakSelf loadMore];
    }];
    
    
    [self.tableView triggerPullToRefresh];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return searchRsult.rsultArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeSearchResultCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeSearchResultCell" forIndexPath:indexPath];

    NSMutableAttributedString *ma = [[NSMutableAttributedString alloc] initWithString:@"NSStrikethroughStyleAttributeName"];
    [ma addAttribute:NSStrikethroughStyleAttributeName value:@2 range:NSMakeRange(0, @"NSStrikethroughStyleAttributeName".length)];
    cell.name.attributedText = ma;
    
//    cell.name.text = @"NSStrikethroughStyleAttributeName";
    
    return cell;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - http delegate -

-(void)requestCompleteWithObj:(WYHttpTask *)obj{

    [mb hide:YES afterDelay:.8];
    id rsult = [obj parese];
    
    if([obj isKindOfClass:[TaskWithHomeSearch class]]){
        
        if(self.tableView.infiniteScrollingView.state == SVInfiniteScrollingStateLoading){
            
        }else if(self.tableView.pullToRefreshView.state == SVPullToRefreshStateLoading){
            
            [searchRsult.rsultArray removeAllObjects];
        }
        
        if(!searchRsult.rsultArray.count){
        
            searchRsult = rsult;
        }else{
            SearchRsult *r = rsult;
            searchRsult.count = r.count;
            [searchRsult.rsultArray addObjectsFromArray:r.rsultArray];
        }
        [self.tableView reloadData];
    }
    [self.tableView.infiniteScrollingView stopAnimating];
    [self.tableView.pullToRefreshView stopAnimating];
}
-(void)requestFailWithMsg:(WYHttpTask *)obj{
    DLog(@"%@",obj.networkOp.url);
    [mb hide:YES afterDelay:.8];
    [self.tableView.infiniteScrollingView stopAnimating];
    [self.tableView.pullToRefreshView stopAnimating];
}

#pragma mark - btn action -

- (IBAction)on_btn:(UIButton *)sender {
    
    switch (sender.tag) {
        case 1:
            //            @all
            break;
        case 2:
            //            @all
            break;
        case 3:
            //            @all
            break;
        case 4:
            //            @all
            break;
        case 5:
            //            @all
            break;
        case 6:
            //            @all
            break;
            
        default:
            break;
    }

}

#pragma mark - private method -

-(void)refreshNew{
    mb = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    TaskWithHomeSearch *search = [[TaskWithHomeSearch alloc] init];
    search.startNum = @"0";
    search.key = self.key;
    search.delegate = self;
    [search start];
}


-(void)loadMore{
    
    mb = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    TaskWithHomeSearch *search = [[TaskWithHomeSearch alloc] init];
    search.startNum = [NSString stringWithFormat:@"%d",searchRsult.rsultArray.count];
    search.key = self.key;
    search.delegate = self;
    [search start];
}

@end
